package com.wh1200.mybatis.xmlless.util

import java.util.concurrent.ConcurrentHashMap


/**
 *
 * @author 吴昊
 * @date 2024/1/16 15:38
 * @since v0.0.0
 * @version 1.0
 */
object ClassUtils {

  private val map: MutableMap<Class<*>, List<Class<*>>> = ConcurrentHashMap()

  fun getAllClassOrInterface(type: Class<*>): List<Class<*>> {
    if (map.containsKey(type)) {
      return map[type]!!
    }
    val list = mutableListOf<Class<*>>()
    var tmp: Class<*>? = type
    while (tmp != Any::class.java && tmp != null) {
      list.add(tmp)
      list.addAll(tmp.interfaces)
      tmp.interfaces.forEach {
        list.addAll(getAllClassOrInterface(it))
      }
      tmp = tmp.superclass
    }
    val result = list.distinct()
    map[type] = result
    return result
  }

}
